package com.jy.appmarket.utils;

import android.content.Context;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.inputmethod.InputMethodManager;

/**
 * 
 * @author xinhui
 *
 */
public class UITools {
	public static void hideSoftInput(View view) {
		final InputMethodManager im = (InputMethodManager) view.getContext()
				.getSystemService(Context.INPUT_METHOD_SERVICE);
		im.hideSoftInputFromWindow(view.getWindowToken(), 0);
	}
	
	public static int[] getDeviceScreenWidthHeight(Context context) {
		DisplayMetrics dm = context.getResources().getDisplayMetrics();
		int[] wh = new int[2];
		wh[0] = dm.widthPixels;
		wh[1] = dm.heightPixels;
		return wh;
	}
	
	public static String normalizationFileSizeToM(long bytes, boolean withUnit){
		if(bytes <= 0){
			return "0";
		}
		
		float mb = bytes / (1024.0f * 1024);
		String smb = null;
		if(withUnit){
			smb = String.format("%.1f", mb) + "M";
		}else{
			smb = String.format("%.1f", mb);
		}
		
		return smb;
		
	}
	
	public static String normalizationFileSizeToK(long bytes, boolean withUnit){
		if(bytes <= 0){
			return "0";
		}
		
		float kb = bytes / 1024.0f;
		String skb = null;
		if(withUnit){
			skb = String.format("%.1f", kb) + "K";
		}else{
			skb = String.format("%.1f", kb);
		}
		return skb;
	}
	
	public static String normalizationFileSize(long bytes){
		if(bytes <= 0){
			return "0K";
		}
		
		if(bytes > 0 && bytes < 1024 * 1024){
			float kb = bytes / 1024.0f;
			String skb = String.format("%.1f", kb) + "K";
			return skb;
		}else{
			float mb = bytes / (1024.0f * 1024);
			String smb = String.format("%.1f", mb) + "M";
			return smb;
		}
	}
	
	/**
	 * 该方法用于将dip大小转换为px大小
	 * @param context	上下文对�?
	 * @param dipValue	dip大小�?
	 * @return  转换后的px像素�?
	 */
	public static int dip2px(Context context, float dipValue){  
        final float scale = context.getResources().getDisplayMetrics().density;  
        return (int)(dipValue * scale + 0.5f);  
    }  
    /**
     * 该方法用于将px大小转换为dip大小  
     * @param context  上下文对�?
     * @param pxValue  px大小�?
     * @return	转换后的dip�?
     */
	public static int px2dip(Context context, float pxValue){  
        final float scale = context.getResources().getDisplayMetrics().density;  
        return (int)(pxValue / scale + 0.5f);  
    }  
	
}
